package com.zy.mc_module.base.kotlin

import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.zy.mc_module.base.App
import com.zy.mc_module.utils.*
import java.math.BigDecimal

class BaseViewHolder<VB : ViewBinding>(_binding: VB) : RecyclerView.ViewHolder(_binding.root) {
    var binding = _binding
    private var mConvertView: View? = binding.root
    private var viewSparseArray: SparseArray<View?> = SparseArray()

    /**
     * 订单有积分的时候设置金额
     */
    fun setOrderPrice(textView: TextView, isExchange: Int, price: Any?, exchangeInfo: String?) {
        if (isExchange == 1) {
            //可以用积分兑换,显示积分
            textView.text = exchangeInfo
        } else {
            //显示金额
            textView.text = ("¥" + PriceUtils.decimalsT(price))
        }
    }

    /**
     * 设置总价格
     */
    fun setAllPrice(textView: TextView, allPrice: BigDecimal, allIntegral: BigDecimal, freight: String?, integralTypeName: String) {
        val str: String
        val price = "¥" + AppTools.decimalsT(ArithUtils.add(allPrice, freight))
        val integral = AppTools.removeTrim(allIntegral) + integralTypeName
        str = if (allIntegral.toInt() == 0) {
            //积分为0
            price
        } else if (allPrice.toInt() == 0) {
            //价格为0
            if (java.lang.Double.valueOf(freight) == 0.0) {
                //运费为0
                integral
            } else {
                "$price+$integral"
            }
        } else {
            "$price+$integral"
        }
        textView.text = (str)
    }

    /***
     * 设置金额,前面加符号 + -
     */
    fun setTextSym(textView: TextView, str: String, sym: String) {
        if ("0.00" == str.replace("¥", "")) {
            textView.text = str
        } else {
            textView.text = sym + str
        }
    }

    /**
     * 设置图片
     */
    fun setImg(imageView: ImageView, drawable: Int) {
        GlideUtil.displayImage(imageView, drawable)
    }

    /**
     * 设置图片
     */
    fun setImg(imageView: ImageView, path: String?) {
        GlideUtil.displayImage(imageView, path)
    }

    /**
     * 设置图片
     */
    fun setImg(imageView: ImageView, path: String?, errorImg: Int) {
        GlideUtil.displayImage(imageView, path, errorImg)
    }

    /**
     * 设置控件文字
     */
    fun setText(textView: TextView, charSequence: CharSequence?) {
        if (null != charSequence) {
            textView.text = charSequence
        }
    }

    /**
     * 设置控件文字,如果内容为null,则隐藏
     */
    fun setTextVis(textView: TextView, charSequence: CharSequence?) {
        if (null == charSequence || charSequence.toString().contains("null")) {
            textView.visibility = View.GONE
        } else {
            textView.visibility = View.VISIBLE
            textView.text = charSequence
        }
    }

    /**
     * 设置文字颜色
     */
    fun setTextColor(view: TextView, color: Int) {
        view.setTextColor(color)
    }

    /**
     * 获取控件文字
     */
    fun getTextCon(textView: TextView): String? {
        return textView.text.toString().trim()
    }

    /**
     * 设置控件文字和颜色
     */
    fun setTextC(textView: TextView, charSequence: CharSequence?, color: Int) {
        textView.text = charSequence
        textView.setTextColor(color)
    }

    /***
     * 设置控件文字和图片
     * @param id
     * @param charSequence 文本信息
     * @param drawable 图片
     * @param orientation 图片位置 1左/2上/3右/4下
     */
    fun setTextD(textView: TextView, charSequence: CharSequence?, drawable: Int, orientation: Int) {
        textView.text = charSequence
        when (orientation) {
            1 -> textView.setCompoundDrawablesWithIntrinsicBounds(drawable, 0, 0, 0)
            2 -> textView.setCompoundDrawablesWithIntrinsicBounds(0, drawable, 0, 0)
            3 -> textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawable, 0)
            4 -> textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, drawable)
        }
    }

    /**
     * 设置控件是否显示
     */
    fun setVisible(view: View, visible: Boolean) {
        view.visibility = if (visible) View.VISIBLE else View.GONE
    }

    /**
     * 设置控件是否显示
     */
    fun setGone(view: View, gone: Boolean) {
        view.visibility = if (gone) View.VISIBLE else View.INVISIBLE
    }

    /**
     * 设置控件宽度
     */
    fun setWidth(view: View, width: Int) {
        val para = view.layoutParams
        para.width = width
        view.layoutParams = para
    }

    /**
     * 设置控件高度
     *
     * @param height -1铺满/-2包裹
     */
    fun setHeight(view: View, height: Int) {
        val para = view.layoutParams
        para.height = height
        view.layoutParams = para
    }

    /**
     * 设置控件宽高
     */
    fun setWidthHeight(view: View, width: Int, height: Int) {
        val para = view.layoutParams
        para.width = width
        para.height = height
        view.layoutParams = para
    }

    /**
     * 设置控件padding
     */
    fun setPadding(view: View, left: Int, top: Int, right: Int, bottom: Int) {
        view.setPadding(
                SizeUtils.dp2px(App.getAppContext(), left.toFloat()),
                SizeUtils.dp2px(App.getAppContext(), top.toFloat()),
                SizeUtils.dp2px(App.getAppContext(), right.toFloat()),
                SizeUtils.dp2px(App.getAppContext(), bottom.toFloat()))
    }

    /**
     * 设置控件间隔
     */
    fun setMargin(view: View, l: Int, t: Int, r: Int, b: Int) {
        if (view.layoutParams is ViewGroup.MarginLayoutParams) {
            val p = view.layoutParams as ViewGroup.MarginLayoutParams
            p.setMargins(l, t, r, b)
            view.requestLayout()
        }
    }

    /**
     * 设置子控件的点击事件
     */
    fun setOnClickListener(view: View, listener: View.OnClickListener?) {
        if (listener != null) {
            view.setOnClickListener(listener)
        }
    }

    /**
     * 设置父控件的点击事件
     */
    fun setOnClickListener(listener: View.OnClickListener?) {
        if (listener != null) {
            mConvertView?.setOnClickListener(listener)
        }
    }

    /**
     * 设置长按控件的点击事件
     */
    fun setOnLongClickListener(listener: View.OnLongClickListener?) {
        if (listener != null) {
            mConvertView?.setOnLongClickListener(listener)
        }
    }
}